#include <stdio.h>
#include <stdlib.h>

/**
 * 头插法实现单链表
 */
struct Book {
	char title[128];
	float price;
	char author[50];
	struct Book * next;
};

void recordBook(struct Book **);
void printBook(struct Book *);

void recordBook(struct Book **library){
	struct Book *book, *temp;
	book = (struct Book *)malloc(sizeof(struct Book));
	if(book == NULL){
		printf("内存分配失败\n");
		exit(1);
	}
	printf("请输入书名:\n");
	scanf("%[^\n]", book->title);

	printf("请输入售价:\n");
	scanf("%f[^\n]", &book->price);
	getchar();

	printf("请输入作者:\n");
	scanf("%[^\n]", book->author);
	getchar();
	if(*library == NULL){
		*library = book;
		book->next = NULL;
	}else{
		temp = *library;
		*library = book;
		book->next = temp;
	}
}

void printBook(struct Book *library){
	while(1){
		if(library == NULL){
			printf("书库为空\n");
			break;
		}else{
			printf("书名:%s\n", library->title);
			printf("售价:%.2f\n", library->price);
			printf("作者:%s\n\n", library->author);
			library = library->next;
		}
	}
}

int main(void){
	struct Book book;
	struct Book *header=NULL;

	while (1){
		char need;
		printf("Do you want to record one more book? (y/n)\n");
		scanf("%c", &need);
		getchar();

		if('n'==need){
			break;
		}else if ('y' == need){
			recordBook(&header);
		}else{
			printf("Invalid Input parameters!\n");
		}
	}

	printBook(header);
	return 0;
}